#!/bin/sh
################################################################################
# lircd_helper
#
# This script can be used by udev to start or stop lircd when a remote control
# device is added or removed.
#
# lircd_helper configures lircd to output using a uinput event device so that
# eventlircd can aggregate the outputs into a single lircd socket.
#
# lircd_helper configures lircd to use an lircd socket name that is derived
# from the device name. In addition, lircd_helper creates symbolic links to this
# lircd socket that are derived from the device symbolic links. You can use this
# socket and the symbolic link to this socket when using commands such as
# irsend.
#
# lircd_helper understands two lircd_helper specific device properites set
# using ENV{} and passed as environment variables:
#   lircd_driver:
#     Used to tell lircd_helper the name of the lircd driver.
#   lircd_conf:
#     Used to tell lircd_helper the path to the lircd.conf file to use.
#   lircd_uinput_disable:
#     Used to tell lircd_helper to not not have lircd create a uninput event
#     device. Kernel LIRC drivers that use rc_core perform kernel space signal
#     decoding and create an event device. Therefore, we do not want lircd to
#     create another event device. However, if the LIRC device is a transmitter
#     as well as receiver, then we still need lircd running.
################################################################################

case "${ACTION}" in
    add)
        if test "x${lircd_driver}" = "x" ; then
            exit 1;
        fi
        if test "x${lircd_conf}" = "x" ; then
            exit 1;
        fi
        if test ! -e '/var/run/lirc' ; then
            mkdir -p '/var/run/lirc'
        fi
        devname_instance=`echo ${DEVNAME} | sed -e 's/\/\+/~/g' -e 's/^~dev~//'`
        if test ! -e "/var/run/lirc/lircd-${devname_instance}.pid" ; then
            daemon='/usr/sbin/lircd'
            daemon="${daemon} --driver=${lircd_driver}"
            daemon="${daemon} --device=${DEVNAME}"
            if test ! "x${lircd_uinput_disable}" = "xtrue" ; then
                daemon="${daemon} --uinput"
            fi
            daemon="${daemon} --output=/var/run/lirc/lircd-${devname_instance}"
            daemon="${daemon} --pidfile=/var/run/lirc/lircd-${devname_instance}.pid"
            daemon="${daemon} --release=:UP"
            daemon="${daemon} ${lircd_conf}"
            ${daemon}
            for devlink in ${DEVLINKS} ; do
                devlink_instance=`echo ${devlink} | /bin/sed -e 's/\/\+/~/g' -e 's/^~dev~//'`
                rm -f "/var/run/lirc/lircd-${devlink_instance}"
                ln -s "lircd-${devname_instance}" "/var/run/lirc/lircd-${devlink_instance}"
            done
        fi
        ;;
    remove)
        instance=`echo $DEVNAME | sed -e 's/\/\+/~/g' -e 's/^~dev~//'`
        if test -e "/var/run/lirc/lircd-${instance}.pid" ; then
            pid=`cat /var/run/lirc/lircd-${instance}.pid`
            if test ! "x${pid}" = "x" ; then
                kill ${pid}
            fi
            for devlink in ${DEVLINKS} ; do
                devlink_instance=`echo ${devlink} | sed -e 's/\/\+/~/g' -e 's/^~dev~//'`
                rm -f "/var/run/lirc/lircd-${devlink_instance}"
            done
        fi
        ;;
esac

exit 0
